<template>
  <j-modal
    :title="title"
    :width="1000"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="会员等级">
          <a-input-number v-decorator="[ 'level', {}]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="等级名称">
          <a-input placeholder="请输入等级名称" v-decorator="['name', {}]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="需要成长值">
          <a-input-number v-decorator="[ 'minGrowth', {}]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="是否为默认等级">
          <j-dict-select-tag v-decorator="['defaultStatus', {}]" placeholder="默认等级" :type="'radioButton'"
                             :triggerChange="true" dictCode="yn"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="获取积分倍数">
          <a-input-number v-decorator="[ 'integralPoint', {}]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="升级礼包券码">
          <a-input placeholder="请输入升级礼包券码" v-decorator="['upgradeGift', {}]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="生日礼包券码">
          <a-input placeholder="请输入生日礼包券码" v-decorator="['birthdayGift', {}]"/>
        </a-form-item>
        <a-row class="form-row" :gutter="24">
          <a-col :span="12">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          style="margin-right: -485px"
          label="会员卡背景">
          <a-upload
            listType="picture-card"
            class="logo-uploader"
            :showUploadList="false"
            :action="uploadAction"
            :data="{'isup':1,'dir':'cms/level'}"
            :headers="headers"
            :beforeUpload="beforeUpload"
            @change="handleChange"
          >
            <img v-if="picUrl" :src="getLogoView()" alt="logo" style="height:104px;max-width:300px"/>
            <div v-else>
              <a-icon :type="uploadLoading ? 'loading' : 'plus'" />
              <div class="ant-upload-text">上传</div>
            </div>
            <span class="ant-input-span">尺寸:662*349</span>
          </a-upload>
        </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              style="margin-right: -285px"
              label="会员中心等级标识">
              <a-upload
                listType="picture-card"
                class="logo-uploader"
                :showUploadList="false"
                :action="uploadAction"
                :data="{'isup':1,'dir':'cms/level'}"
                :headers="headers"
                :beforeUpload="beforeUpload"
                @change="handleIconChange"
              >
                <img v-if="iconUrl" :src="getIconView()" alt="logo" style="height:104px;max-width:300px"/>
                <div v-else>
                  <a-icon :type="uploadIconLoading ? 'loading' : 'plus'" />
                  <div class="ant-upload-text">上传</div>
                </div>
                <span class="ant-input-span">尺寸:80*80</span>
              </a-upload>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="24">
          <a-col :span="12">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          style="margin-right: -485px"
          label="成长值等级标识">
          <a-upload
            listType="picture-card"
            class="logo-uploader"
            :showUploadList="false"
            :action="uploadAction"
            :data="{'isup':1,'dir':'cms/level'}"
            :headers="headers"
            :beforeUpload="beforeUpload"
            @change="handleCardChange"
          >
            <img v-if="cardUrl" :src="getCardView()" alt="logo" style="height:104px;max-width:300px"/>
            <div v-else>
              <a-icon :type="uploadCardLoading ? 'loading' : 'plus'" />
              <div class="ant-upload-text">上传</div>
            </div>
          </a-upload>
          <span class="ant-input-span">尺寸:448*104</span>
        </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              style="margin-right: -285px"
              label="会员中心等级入口">
              <a-upload
                listType="picture-card"
                class="logo-uploader"
                :showUploadList="false"
                :action="uploadAction"
                :data="{'isup':1,'dir':'cms/level'}"
                :headers="headers"
                :beforeUpload="beforeUpload"
                @change="handleTagChange"
              >
                <img v-if="tagUrl" :src="getTagView()" alt="logo" style="height:104px;max-width:300px"/>
                <div v-else>
                  <a-icon :type="uploadTagLoading ? 'loading' : 'plus'" />
                  <div class="ant-upload-text">上传</div>
                </div>
              </a-upload>
              <span class="ant-input-span">尺寸:300*145</span>
            </a-form-item>
          </a-col>
        </a-row>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="备注">
          <a-input placeholder="请输入备注" v-decorator="['note', {}]"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import JDate from '@/components/jeecg/JDate'
import pick from 'lodash.pick'
import moment from 'moment'
import { ACCESS_TOKEN } from "@/store/mutation-types"
import Vue from 'vue'

export default {
  name: 'UmsMemberLevelModal',
  components: {
    JDate
  },
  data() {
    return {
      title: '操作',
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      uploadLoading:false,
      uploadIconLoading:false,
      uploadCardLoading:false,
      uploadTagLoading:false,
      headers:{},
      form: this.$form.createForm(this),
      validatorRules: {},
      picUrl: "",
      iconUrl: "",
      cardUrl: "",
      tagUrl: "",
      url: {
        add: '/ums/umsMemberLevel/add',
        edit: '/ums/umsMemberLevel/edit',
        umsMemberLevelRightsList: '/ums/umsMemberLevel/queryUmsMemberLevelRightsByMainId',
        umsMemberLevelBannerList: '/ums/umsMemberLevel/queryUmsMemberLevelBannerByMainId',
        fileUpload: window._CONFIG['domianURL']+"/sys/common/ossUpload",
        imgerver: window._CONFIG['domianURL']+"/sys/common/view",
      }
    }
  },
  created () {
    const token = Vue.ls.get(ACCESS_TOKEN);
    this.headers = {"X-Access-Token":token};
  },
  computed:{
    uploadAction:function () {
      return this.url.fileUpload;
    }
  },
  methods: {
    add() {
      this.picUrl = "";
      this.iconUrl = "";
      this.cardUrl = "";
      this.tagUrl = "";
      this.edit({})
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.picUrl = this.model.bgImg;
      this.iconUrl = this.model.icon;
      this.cardUrl = this.model.cardImg;
      this.tagUrl = this.model.tagImg;
      //初始化明细表数据
      console.log(this.model.id)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, 'level', 'name', 'minGrowth', 'defaultStatus', 'integralPoint', 'upgradeGift', 'birthdayGift', 'bgImg', 'note'))
        // 时间格式化
      })
    },
    beforeUpload: function(file){
      var fileType = file.type;
      if(fileType.indexOf('image')<0){
        this.$message.warning('请上传图片');
        return false;
      }
      //TODO 验证文件大小
    },
    handleChange (info) {
      this.picUrl = "";
      if (info.file.status === 'uploading') {
        this.uploadLoading = true;
        return
      }
      if (info.file.status === 'done') {
        var response = info.file.response;
        this.uploadLoading = false;
        console.log(response);
        if(response.success){
          this.model.bgImg = response.message;
          this.picUrl = this.model.bgImg;
        }else{
          this.$message.warning(response.message);
        }
      }
    },
    getLogoView(){
      if(this.model.bgImg !== null && this.model.bgImg !== undefined && this.model.bgImg !== ''){
        if('http' === this.model.bgImg.substring(0,4)){
          return this.model.bgImg;
        }
        return this.url.imgerver +"/"+ this.model.bgImg;
      }
    },
    handleIconChange (info) {
      this.iconUrl = "";
      if (info.file.status === 'uploading') {
        this.uploadIconLoading = true;
        return
      }
      if (info.file.status === 'done') {
        var response = info.file.response;
        this.uploadIconLoading = false;
        console.log(response);
        if(response.success){
          this.model.icon = response.message;
          this.iconUrl = this.model.icon;
        }else{
          this.$message.warning(response.message);
        }
      }
    },
    getIconView(){
      if(this.model.icon !== null && this.model.icon !== undefined && this.model.icon !== ''){
        if('http' === this.model.icon.substring(0,4)){
          return this.model.icon;
        }
        return this.url.imgerver +"/"+ this.model.icon;
      }
    },
    handleCardChange (info) {
      this.cardUrl = "";
      if (info.file.status === 'uploading') {
        this.uploadCardLoading = true;
        return
      }
      if (info.file.status === 'done') {
        var response = info.file.response;
        this.uploadCardLoading = false;
        console.log(response);
        if(response.success){
          this.model.cardImg = response.message;
          this.cardUrl = this.model.cardImg;
        }else{
          this.$message.warning(response.message);
        }
      }
    },
    getCardView(){
      if(this.model.cardImg !== null && this.model.cardImg !== undefined && this.model.cardImg !== ''){
        if('http' === this.model.cardImg.substring(0,4)){
          return this.model.cardImg;
        }
        return this.url.imgerver +"/"+ this.model.cardImg;
      }
    },
    handleTagChange (info) {
      this.tagUrl = "";
      if (info.file.status === 'uploading') {
        this.uploadTagLoading = true;
        return
      }
      if (info.file.status === 'done') {
        var response = info.file.response;
        this.uploadTagLoading = false;
        console.log(response);
        if(response.success){
          this.model.tagImg = response.message;
          this.tagUrl = this.model.tagImg;
        }else{
          this.$message.warning(response.message);
        }
      }
    },
    getTagView(){
      if(this.model.tagImg !== null && this.model.tagImg !== undefined && this.model.tagImg !== ''){
        if('http' === this.model.tagImg.substring(0,4)){
          return this.model.tagImg;
        }
        return this.url.imgerver +"/"+ this.model.tagImg;
      }
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values)
          //时间格式化
          httpAction(httpurl, formData, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
            that.close()
          })
        }
      })
    },
    handleCancel() {
      this.close()
    }
  }
}
</script>

<style scoped>
.ant-btn {
  padding: 0 10px;
  margin-left: 3px;
}

.ant-form-item-control {
  line-height: 0px;
}

/** 主表单行间距 */
.ant-form .ant-form-item {
  margin-bottom: 10px;
}

/** Tab页面行间距 */
.ant-tabs-content .ant-form-item {
  margin-bottom: 0px;
}
</style>